package com.example.tic_tac_toe_in_android.view;

import java.util.Locale;

import com.example.tic_tac_toe_in_android.R;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.DownloadManager.Request;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	// buttons
	Button bTOnePlayer;
	Button bTTwoPlayer;
	Button bTOption;
	Button bTSound;
	private static final int REQUEST_CHANGE_LANGUAGE = 1;
	public static String selectedLanguage = "";
	private static Configuration c;

	// Menus
	Menu optionMenu;

	// menu Items
	MenuItem itemSound;
	MenuItem itemAbout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		allComponentInit();
		c = new Configuration(getResources().getConfiguration());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.option_menu, menu);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.option_menu, menu);

		// menu
		this.optionMenu = menu;

		// adding menu items
		itemSound = menu.findItem(R.id.itemSound);
		itemAbout = menu.findItem(R.id.itemAbout);

		return true;
	}

	/*
	 * context Menu
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.sound_context_menu, menu);
	}

	/*
	 * buttons
	 */
	public void onClick(View v) {

		if (v == bTOnePlayer) {
			Toast.makeText(this, "Not imlemented yet", Toast.LENGTH_LONG)
					.show();
		} else if (v == bTTwoPlayer) {
			Intent test = new Intent(this, OptionMenuActivity.class);
			startActivity(test);

		} else if (v == bTOption) {
			Intent test = new Intent(this, Languages.class);
			startActivityForResult(test, REQUEST_CHANGE_LANGUAGE);
		} else {
			System.out.println("EROOOOOOR");
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK && requestCode == REQUEST_CHANGE_LANGUAGE) {

			// Configuration c = new Configuration(getResources()
			// .getConfiguration());
			// c = new Configuration(getResources().getConfiguration());

			// String language = data.getStringExtra("language");
			selectedLanguage = data.getStringExtra("language");

			if (selectedLanguage.equals("English")) {

				c.locale = Locale.ENGLISH;

			} else if (selectedLanguage.equals("Danish")) {

				c.locale = Locale.GERMAN;

			}

			getResources().updateConfiguration(c,
					getResources().getDisplayMetrics());

			Intent intent = getIntent();

			overridePendingTransition(0, 0);

			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

			finish();

			overridePendingTransition(0, 0);

			startActivity(intent);

		}

	}

	// Menu
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.itemSound:
			// startActivity(new Intent(Intent.ACTION_VIEW,
			// Uri.parse("http://amazon.com")));
			Dialog soundDialog = new Dialog(this);
			soundDialog.setContentView(R.layout.sound_dialog);
			soundDialog.setTitle("Sound Options");
			soundDialog.show();
			break;

		case R.id.itemAbout:
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://en.wikipedia.org/wiki/Tic-tac-toe")));
			break;

		default:
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://google.com")));
			break;
		}

		// if (item == sound) {
		// startActivity(new Intent(Intent.ACTION_VIEW,
		// Uri.parse("http://youtube.com")));
		// } else {
		// startActivity(new Intent(Intent.ACTION_VIEW,
		// Uri.parse("http://google.com")));
		// }

		return super.onOptionsItemSelected(item);
	}

	private void allComponentInit() {

		bTOnePlayer = (Button) findViewById(R.id.BTOnePlayer);
		bTOnePlayer.setOnClickListener(this);

		bTTwoPlayer = (Button) findViewById(R.id.BTTwoPlayer);
		bTTwoPlayer.setOnClickListener(this);

		bTOption = (Button) findViewById(R.id.BTOption);
		bTOption.setOnClickListener(this);

		bTSound = (Button) findViewById(R.id.BTSound);
		bTSound.setOnClickListener(this);

		// context Menu
		registerForContextMenu(bTOption);

	}
}
